#include "02_TaskQueue.hpp"

TaskQueue::TaskQueue(int que_size)
//    :_que(new queue<int> que)
    :_que(queue<int>())
    ,_que_size(que_size)
    ,_mutex()
    ,_notfull()
    ,_notempty(){}

//TaskQueue::TaskQueue(int que_size) = default;

bool TaskQueue::empty()
{
    return _que.size() == 0;
}

bool TaskQueue::full()
{
    return static_cast<int>(_que.size()) == _que_size;
}

void TaskQueue::push(int data)
{
    _mutex.lock();

    while(full())
    {
        _notfull.wait(_mutex);
    }

    _que.push(data);
    
    _notfull.notify_one();

    _mutex.unlock();
}

int TaskQueue::pop()
{
    _mutex.lock();

    while(empty())
    {
        _notempty.wait(_mutex);
    }

    int data = _que.front();
    _que.pop();

    _notfull.notify_one();

    return data;
}
